<?php

namespace My\Controller;

use Utuo\BackgroundController;

class HouseSubscribeController extends BackgroundController {
	
	public function _initialize() {
		$this->setControllerTitle ( "预约管理" );
		$this->setActionTitle ( array (
				'index' => '预约内容编辑',
				'userList' => '预约用户列表'
		) );
		$this->setActionNav ( array (
				'index',
				'userList',
		) );
	}
	
	// 预约编辑
	public function indexAction() {
		$d = D('HouseSubscribe');
		$houseSubscribe = $d->find($this->aid);
		if(!$houseSubscribe){
			$d->add(array(
				'id' => $this->aid,
				'cid' => $this->aid
			));
			$houseSubscribe = $d->find($this->aid);
		}
		$houseSubscribe['status'] = $this->getRadio('status', $d->status, $houseSubscribe['status']);
		$this->assign("houseSubscribe", $houseSubscribe);
		$this->display();
	}
	
	// 预约提交
	public function indexPostAction() {
		$d = D('HouseSubscribe');
		$d->submit() === false && $this->ajaxError($d->getError());
		$this->ajaxSuccess();
	}
	
	// 预约用户列表
	public function userListAction() {
		$page = $this->getPage ();
		$pagesize = $this->getPageSize ();
		$d = D ( 'HouseSubscribeUser this0' )->join("__HOUSE_TYPE__ this1 on this0.house_type = this1.id",'LEFT')->field("this0.*,this1.name as house_name");
		$condition = array (
			'this0.cid' => $this->aid,
		);
		$houseSubscribeUserLists = $d->where ( $condition )->limit ( $pagesize )->page ( $page )->order ( 'id DESC' )->select ();
		$houseSubscribeUserCount = $d->where ( $condition )->count ();
		foreach ($houseSubscribeUserLists as &$v) {
			$v['statusName'] = $d->status[$v['status']];
		}
		$this->assign ( 'houseSubscribeUserLists', $houseSubscribeUserLists );
		$this->showPage ( $houseSubscribeUserCount, $pagesize );
		$this->display ();
	}
	
	public function deleteUserAction() {
		$this->ajaxDelete("HouseSubscribeUser");
	}
}